package com.lovo.invoicing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.invoicing.bean.ClientBean;
import com.lovo.invoicing.bean.PageBean;
import com.lovo.invoicing.mapper.ClientMapper;
import com.lovo.invoicing.service.IClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户服务层
 */
@Service
public class ClientServiceImpl extends ServiceImpl<ClientMapper, ClientBean> implements IClientService {
    @Autowired
    ClientMapper clientMapper;
    @Override
    @Transactional
    public void addClient(ClientBean clientBean) {
        baseMapper.insert(clientBean);
    }

    @Override
    public ClientBean queryClientById(int clientId) {
        return baseMapper.selectById(clientId);
    }

    @Override
    @Transactional
    public void updateClient(ClientBean clientBean) {
       baseMapper.updateById(clientBean);
    }

    @Override
    public IPage<ClientBean> queryClientsByPage(PageBean<ClientBean> pageBean) {
        QueryWrapper<ClientBean> qw = new QueryWrapper<>();
        ClientBean c = pageBean.getT();
        if (c.getClientName()!=null && !"".equals(c.getClientName())){
            qw.like("client_name",c.getClientName());
        }
        if (c.getClientScope()!=null && !"".equals(c.getClientScope())){
            qw.like("client_scope",c.getClientScope());
        }
        return baseMapper.selectPage(new Page<>(pageBean.getPageNo(), pageBean.getPageSize()),qw);
    }

    @Override
    public List<ClientBean> queryAllClients() {
        return baseMapper.selectList(null);
    }

}
